package cn.fanyamin.web.struts;


import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.LazyValidatorForm;

import cn.fanyamin.business.RoleService;
import cn.fanyamin.domain.Module;
import cn.fanyamin.domain.Role;

public class RoleAction extends SecureBaseAction {
	private static Log logger = LogFactory.getLog(RoleAction.class);

	public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
		RoleService roleService = getWebApp().getRoleService();
	    List<Role> roleList = roleService.list();
	    request.setAttribute("roleList", roleList);
		logger.info("roleList: " + roleList);
        return mapping.findForward("list");

	}
	public ActionForward create(ActionMapping mapping, ActionForm form,
	                            HttpServletRequest request, HttpServletResponse response) {
		RoleService roleService = getWebApp().getRoleService();
		Role role = getRoleFromForm(form);
		if(role == null) {
		    return mapping.findForward("failure");
		}
		int roleID = roleService.create(role);
		role.setRoleID(roleID);
		logger.info("create role: " + role);
		return mapping.findForward("success");
	}
	
    private Role getRoleFromForm(ActionForm form) {
		Role role = new Role();
		LazyValidatorForm roleForm = (LazyValidatorForm) form;

		role.setRoleID(NumberUtils.toInt((String) roleForm.get("roleID")));
		role.setRoleName((String) roleForm.get("roleName"));		
		role.setDescription((String) roleForm.get("description"));	
		String strModuleIDs = (String)roleForm.get("strModuleID");
		String[] arrModuleID = strModuleIDs.split(" ");
		for(String str: arrModuleID) {
			int moduleID = NumberUtils.toInt(str);
		    Module module = new Module();
		    module.setModuleID(moduleID);
		    role.addModule(module);
		}
		
		return role;
	}

    public ActionForward retrieve(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        RoleService roleService = getWebApp().getRoleService();
        LazyValidatorForm roleFrom = (LazyValidatorForm)form;
        int roleID = NumberUtils.toInt((String)roleFrom.get("roleID"));
        Role role = roleService.retrieve(roleID);
        request.setAttribute("role", role);
        return mapping.findForward("edit");
    }
    
    public ActionForward update(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        
        RoleService roleService = getWebApp().getRoleService();
        Role role = getRoleFromForm(form);
        roleService.update(role);
        logger.info("update role, role=" + role);
        return mapping.findForward("success");
    }
    
    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        RoleService roleService = getWebApp().getRoleService();
        LazyValidatorForm roleFrom = (LazyValidatorForm)form;
        int roleID = NumberUtils.toInt((String)roleFrom.get("roleID"));
        roleService.delete(roleID);
        logger.info("delete role, roleID=" + roleID);
        return mapping.findForward("success");
    }
    
    public ActionForward add(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
    	logger.info("add role");
    	RoleService roleService = getWebApp().getRoleService();
    	List<Module> moduleList = roleService.getAllModuleList();
    	request.setAttribute("allModuleList", moduleList);
    	return mapping.findForward("add");
    }
    
    public ActionForward edit(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        RoleService roleService = getWebApp().getRoleService();
        LazyValidatorForm roleFrom = (LazyValidatorForm)form;
        int roleID = NumberUtils.toInt((String)roleFrom.get("roleID"));
        Role role = roleService.retrieve(roleID);
        request.setAttribute("role", role);
        return mapping.findForward("edit");
    }
}
